/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.jee.lib.bo;

import java.util.ArrayList;
import uit.jee.lib.access.mapper.BookMapper;
import uit.jee.lib.access.mapper.BookTypeMapper;
import uit.jee.lib.dto.BookDTO;

/**
 *
 * @author ngloc_it
 */
public class BookBO {
    private BookMapper mapper;

    public BookBO() throws Exception{
        mapper = new BookMapper();
    }

    public boolean CheckExitedBook(BookDTO book) throws Exception{
        if (book == null){
            throw new Exception("Book is null.");
        }
        return mapper.CheckExitedBook(book);
    }

    public ArrayList<BookDTO> getAllBook() throws Exception{        
        return mapper.getAllBook();
    }


    public ArrayList<BookDTO> getTopN(int start, int n) throws Exception{
        return mapper.getTopN(start, n);
    }

    public ArrayList<BookDTO> getTopTen() throws Exception{        
        return mapper.getTopTen();
    }

    public BookDTO getRandomBook() throws Exception{
        BookMapper bookMapper = new BookMapper();
        return bookMapper.getRandomBook();
    }

    public BookDTO getBookById(String bookId) throws Exception{
        BookMapper bookMapper = new BookMapper();
        BookTypeMapper bookTypeMapper = new BookTypeMapper();
        BookDTO book = bookMapper.getBookById(bookId);
        if(book == null)
            return null;
        String bookTypeId = book.getBookTypeId();
        String bookTypeName = bookTypeMapper.GetBookTypeName(bookTypeId);
        book.setBookType(bookTypeName);
        
        return book;
    }

    public ArrayList<BookDTO> getBookByName(String bookName) throws Exception{
        BookMapper bookMapper = new BookMapper();
        BookTypeMapper bookTypeMapper = new BookTypeMapper();
        ArrayList<BookDTO> books = bookMapper.getBookByName(bookName);
        if((books == null) ||
                (
                    (books != null) && (books.isEmpty()))
                ){
            return null;
        }

        int n = books.size();
        for(int i = 0; i < n; i++){
            String bookTypeName = bookTypeMapper.
                                    GetBookTypeName(books.get(i).
                                    getBookTypeId());
            books.get(i).setBookType(bookTypeName);
        }
        
        return books;
    }

    public ArrayList<BookDTO> getBookByAuthorName(String authorName) throws Exception{
        BookMapper bookMapper = new BookMapper();
        BookTypeMapper bookTypeMapper = new BookTypeMapper();

        ArrayList<BookDTO> books = bookMapper.getBookByAuthorName(authorName);
        if((books == null) ||
                (
                    (books != null) && (books.isEmpty()))
                ){
            return null;
        }

        int n = books.size();
        for(int i = 0; i < n; i++){
            String bookTypeName = bookTypeMapper.
                                    GetBookTypeName(books.get(i).
                                    getBookTypeId());
            books.get(i).setBookType(bookTypeName);
        }

        return books;
    }

    public ArrayList<BookDTO> getBookByAuthorId(String authorId) throws Exception{
        BookMapper bookMapper = new BookMapper();
        BookTypeMapper bookTypeMapper = new BookTypeMapper();

        ArrayList<BookDTO> books = bookMapper.getBookByAuthorId(authorId);
        if((books == null) ||
                (
                    (books != null) && (books.isEmpty()))
                ){
            return null;
        }

        int n = books.size();
        for(int i = 0; i < n; i++){
            String bookTypeName = bookTypeMapper.
                                    GetBookTypeName(books.get(i).
                                    getBookTypeId());
            books.get(i).setBookType(bookTypeName);
        }

        return books;
    }

    public ArrayList<BookDTO> getBookByBookTypeName(String bookTypeName) throws Exception{
        BookMapper bookMapper = new BookMapper();
        BookTypeMapper bookTypeMapper = new BookTypeMapper();

        ArrayList<BookDTO> books = bookMapper.getBookByBookTypeName(bookTypeName);
        if((books == null) ||
                (
                    (books != null) && (books.isEmpty()))
                ){
            return null;
        }

        int n = books.size();
        for(int i = 0; i < n; i++){
            books.get(i).setBookType(bookTypeName);
        }

        return books;
    }

    public ArrayList<BookDTO> getBookByBookTypeId(String bookTypeId) throws Exception{
        BookMapper bookMapper = new BookMapper();
        BookTypeMapper bookTypeMapper = new BookTypeMapper();

        ArrayList<BookDTO> books = bookMapper.getBookByBookTypeId(bookTypeId);
        if ((books == null)
                || ((books != null) && (books.isEmpty()))) {
            return null;
        }
        return books;
    }

    public void Insert (BookDTO book) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.Insert(book);
    }

    public void Insert(BookDTO[] book) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.Insert(book);
    }

    public void DeleteById(String bookId) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.DeleteById(bookId);
    }

    public void Delete(BookDTO book) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.Delete(book);
    }

    public void Delete(BookDTO[] books) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.Delete(books);
    }

    public void Update(BookDTO book) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.Update(book);
    }

    public void Update(BookDTO[] books) throws Exception{
        BookMapper mapper = new BookMapper();
        mapper.Update(books);
    }
}
